﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DLMemberPointMS.Common;
using DLMemberPointMS.Entity;
using DLMemberPointMS.Entity.POCOModel;
using DLMemberPointMS.Entity.ViewModel;


namespace DLMemberPointMS.Business
{
    public class ExchangGiftsService : BaseService<ExchangGifts>
    {
        //李晓婉写的礼品管理
        #region 列表查询
        /// <summary>
        /// 查询列表信息
        /// </summary>
        /// <param name="viewModel">带查询条件的分页视图</param>
        /// <returns>返回一个枚举类型</returns>
        public OperateResult GetEGS(PrePage viewModel)
        {
            var query = PredicateExtensions.True<ExchangGifts>();

            int rowCount = 0;
            var pageData = GetList(viewModel.page, viewModel.rows, ref rowCount, query, e => e.EG_ID, true)
                .Select(e => new
                {
                    e.EG_ID,
                    e.EG_GiftCode,
                    e.EG_GiftName,
                    e.EG_Point,
                    e.EG_Number,
                    e.EG_ExchangNum,
                    e.EG_Remark,
            }).ToList();
            if (pageData == null)
            {
                return new OperateResult(code.Error, "没有数据哟！");
            }
            else
            {
                return new OperateResult(code.Success, "查询成功", pageData, rowCount);
            }
         }
        #endregion
        #region 添加
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="model">添加页面创建的视图</param>
        /// <returns></returns>
        public OperateResult InsertMemCards(PreInsertView view)
        {
            if (string.IsNullOrEmpty(view.EG_GiftCode))
            {
                return new OperateResult(code.Error, "礼品代号不能为空");
            }
            if (Find(e => e.EG_GiftCode == view.EG_GiftCode) != null)
            {
                return new OperateResult(code.Error, "该礼品代号已存在");
            }
            if (string.IsNullOrEmpty(view.EG_GiftName))
            {
                return new OperateResult(code.Error, "礼品名称不能为空");
            }
            else
            {
                ExchangGifts model = new ExchangGifts();
                model.EG_GiftCode = view.EG_GiftCode;
                model.EG_GiftName = view.EG_GiftName;
                model.EG_Photo = view.EG_Photo;
                model.EG_Point = view.EG_Point;
                model.EG_Number = view.EG_Number;
                model.EG_Remark = view.EG_Remark;
                if (Add(model))
                {
                    return new OperateResult(code.Success, "礼品添加成功");
                }
                else
                {
                    return new OperateResult(code.Error, "礼品添加失败");
                }
            }
        }
    
        #endregion
        #region 修改
        /// <summary>
        /// 修改前获取数据
        /// </summary>
        /// <param name="id">要获取的数据的ID</param>
        /// <returns></returns>
        public OperateResult GetUpdateEGS(int id)
        {
            var model = Find(e => e.EG_ID == id);
        
            if (model!= null)
            {
                
                PreInsertView view = new PreInsertView();
                view.EG_GiftCode = model.EG_GiftCode;
                view.EG_GiftName = model.EG_GiftName;
                view.EG_Number = Convert.ToInt32(model.EG_Number);
                view.EG_Photo = model.EG_Photo;
                //view.EG_Photo = model.EG_Photo;
                view.EG_Point = Convert.ToInt32(model.EG_Point);
                view.EG_Remark = model.EG_Remark;
                view.EG_ID = model.EG_ID;
                return new OperateResult(code.Success, "修改成功", view);
            }
            else
            {
                return new OperateResult(code.Error, "修改失败");
            }
        }
     
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="view">用户输入的要修改的数据</param>
        /// <returns></returns>
        public OperateResult UpdateEGC(PreInsertView view)
        {
            var model = Find(e => e.EG_ID == view.EG_ID);
            if (model == null)
            {
                return new OperateResult(code.Error, "该数据不存在");
            }
            if (string.IsNullOrEmpty(view.EG_GiftCode))
            {
                return new OperateResult(code.Error, "礼品代号不能为空");
             }
            if (Find(e => e.EG_GiftCode == view.EG_GiftCode) != null)
            {
                return new OperateResult(code.Error, "该礼品代号已存在");
            }
            if (string.IsNullOrEmpty(view.EG_GiftName))
            {
                return new OperateResult(code.Error, "礼品名称不能为空");
            }
            else
            {
                model.EG_ID = view.EG_ID;
                model.EG_GiftCode = view.EG_GiftCode;
                model.EG_GiftName = view.EG_GiftName;
                model.EG_Number = view.EG_Number;
                model.EG_Photo = view.EG_Photo;
                model.EG_Point = view.EG_Point;
                model.EG_Remark = view.EG_Remark;
                if (Update(model))
                {
                    return new OperateResult(code.Success, "修改成功");
                }
                else
                {
                    return new OperateResult(code.Error, "修改失败");

                }
            }
            }
        #endregion
        #region 删除
        public OperateResult DeleEXC(int id)
        {
            var model = Find(e => e.EG_ID == id);
            if (model != null)
            {
           
                if (Delete(model))
                {
                    return new OperateResult(code.Success, "删除成功");
                }
                else
                {
                    return new OperateResult(code.Error, "删除失败");
                }
            }
            else
            {
                return new OperateResult(code.Error, "数据不存在");
            }
        }
        #endregion
    }
}
